package wx.controller;

import admin.bean.BootStrapExpressData;
import admin.bean.Express;
import admin.bean.Message;
import admin.service.ExpressService;
import admin.service.impl.ExpressServiceImpl;
import mvc.ResponseBody;
import mvc.ResponseView;
import util.DateFormatUtil;
import util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QRCodeController {

    ExpressService service = new ExpressServiceImpl();

    @ResponseView("/WxExpress/createQRCode.do")
    public String createQrcode(HttpServletRequest req, HttpServletResponse resp){
        //获取前端数据expressList.html
        String code = req.getParameter("code");
        //express | user
        String type = req.getParameter("type");

        String userPhone = null;
        String qRCodeContent = null;
        if ("express".equals(type)){
            //单个快递二维码，展示单个快递的信息
            qRCodeContent = "expressCode:"+code;
        }else {
            //用户的二维码，展示用户的所有快递

            //获取存储的userPhone的session值
            String[] userInfo = (String[]) req.getSession().getAttribute("userInfo");
            userPhone = userInfo[0];
            qRCodeContent = "userPhone:"+userPhone;
        }

        //存储qrcode信息值到session中
        req.getSession().setAttribute("qrcode",qRCodeContent);

        //相当于跳转页面
        return "/WxExpress/personQRcode.html";

    }

    @ResponseBody("/WxExpress/qrcode.do")
    public String getQRCode(HttpServletRequest req, HttpServletResponse resp){
        //从session中获取存储的qrcode信息
        String qrcode = (String) req.getSession().getAttribute("qrcode");
        Message msg = new Message();
        if (qrcode == null){
            msg.setStatus(-1);
            msg.setResult("取件码出错，请用户重新操作");
        }else {
            msg.setStatus(0);
            msg.setResult(qrcode);
        }

        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/WxExpress/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest req, HttpServletResponse resp){

        String code = req.getParameter("code");
        boolean flag = service.updateStatus(code);
        Message msg = new Message();
        if (flag){
            msg.setStatus(-1);
            msg.setResult("取件成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请用户更新二维码");
        }

        return JsonUtil.toJSON(msg);

    }

    @ResponseBody("/WxExpress/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest req, HttpServletResponse resp){

        String code = req.getParameter("code");
        Express e = service.findByCode(code);
        BootStrapExpressData e1 = null;
        if (e!=null){
            e1 = new BootStrapExpressData(e.getId(),
                    e.getNumber(),e.getUserName(),
                    e.getUserPhone(),e.getCompany(),e.getCode(),
                    DateFormatUtil.format(e.getInTime()),
                    (e.getOutTime()==null?"未出库": DateFormatUtil.format(e.getOutTime())),
                    e.getStatus()==0?"待取件":"已取件",
                    e.getSysPhone());
        }
        Message msg = new Message();
        if (e == null) {
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e1);
        }

        return JsonUtil.toJSON(msg);

    }




}
